using System;
using System.Collections;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;
using InfoJetSoft.Service.Worker;

namespace InfoJetSoft.Service.ManagedCode
{
	/// <summary>
	/// Summary description for Errors.
	/// </summary>
	public class Errors : Microsoft.Office.Interop.InfoPath.SemiTrust.ErrorsCollection
	{
		private Form form = null;
		
		public Errors( Form frm )
		{
			this.form = frm;
		}

		public void Delete(object varNode, string bstrConditionName)
		{
			if( varNode == null || bstrConditionName == null )
			{
				return;
			}
			foreach( FieldError fieldError in this.form.FieldErrorList )
			{
				if( fieldError.ErrorType.Equals( FieldError.ProgramError ) )
				{
					Error error = (Error)fieldError.ProgramErrorObject;

					XmlNode xmlNode = ((IDOMNode)error.Node).XmlNode;
					XmlNode curNode = ((IDOMNode)varNode).XmlNode;
					if( xmlNode.Equals( curNode ) && error.ConditionName.Equals( bstrConditionName ) )
					{
						this.form.FieldErrorList.Remove( fieldError );
						break;
					}
				}
			}
		}

		public Microsoft.Office.Interop.InfoPath.SemiTrust.ErrorObject Add(object varNode, string bstrConditionName, string bstrShortErrorMessage, string bstrDetailedErrorMessage, int lErrorCode, string bstrType)
		{
			if( varNode is IDOMNode )
			{
				IDOMNode domNode = (IDOMNode)varNode;
				Error error = new Error();
				error.Node = domNode;
				error.ConditionName = bstrConditionName;
				error.ShortErrorMessage = bstrShortErrorMessage;
				error.DetailedErrorMessage = bstrDetailedErrorMessage;
				error.ErrorCode = lErrorCode;
				error.ErrorType = bstrType;
				if( error.ErrorType == null )
				{
					error.ErrorType = Error.modeless;
				}

				FieldError fieldError = new FieldError( FieldError.ProgramError, domNode.XmlNode, DOMUtil.getNodeJetId( domNode.XmlNode ), bstrShortErrorMessage + "\n\n" + bstrDetailedErrorMessage );
				fieldError.ProgramErrorObject = error;
				this.form.FieldErrorList.Add( fieldError );
				return error;
			}
			return null;
		}

		public IEnumerator GetEnumerator()
		{
			return null;
		}

		public Microsoft.Office.Interop.InfoPath.SemiTrust.ErrorObject this[int index] 
		{ 
			get
			{
				return null;
			}
		}

		public int Count 
		{ 
			get
			{
                //CID 0778 //2011.03.28 //Begin
                if (this.form.IsDomReadonly)
                {
                    return this.form.FieldErrorList.Count;
                }
                else
                {
                    //0859 //2012.03.15 //Begin
                    return FormIsValidateWorker.Validate(this.form);
                    //0859 //2012.03.15 //End
                }
                //CID 0778 //2011.03.28 //End
			}
		}

		public void DeleteAll()
		{
            //CID 0778 //2011.03.29 //Begin
            this.form.FieldErrorList.Clear();
            //CID 0778 //2011.03.29 //End
		}

	}
}
